﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AssettDatabase
{
    public partial class Form_Modify_Customer : Form
    {
        public Form_Modify_Customer()
        {
            InitializeComponent();
        }

        private void Btn_Return_Main_Click(object sender, EventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        void AutoCompleteText() 
        {
            try
            {
                BLL.Customer bllCustomer = new BLL.Customer();
                AutoCompleteStringCollection strCollection = new AutoCompleteStringCollection();
                TB_Customer_Name.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                TB_Customer_Name.AutoCompleteSource = AutoCompleteSource.CustomSource;
                TB_Customer_Name.AutoCompleteCustomSource = strCollection;

                List<VO.Customer> voCustomerList = bllCustomer.GetCustomers();

                foreach (VO.Customer customer in voCustomerList)
                {
                    strCollection.Add(customer.Customer_Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TB_Customer_Name_TextChanged(object sender, EventArgs e)
        {
        }

        private void Form_Modify_Customer_Load(object sender, EventArgs e)
        {
            AutoCompleteText();
        }

        private void Customer_Search(string Customer_ID)
        {
            try
            {
                BLL.Customer bllCustomer = new BLL.Customer();
                List<VO.Customer> voCustomerList;
                if (Customer_ID != null)
                    voCustomerList = bllCustomer.GetCustomerById(Customer_ID);
                else
                    voCustomerList = bllCustomer.GetCustomerByName(TB_Customer_Name.Text);

                foreach (VO.Customer customer in voCustomerList)
                {
                    MC_TB_Customer_Phone.Text = customer.Customer_Phone;
                    MC_TB_Primary_Contact_FN.Text = customer.Primary_Contact_FN;
                    MC_TB_Customer_ABN.Text = customer.Customer_ABN;
                    MC_TB_Customer_Mob.Text = customer.Customer_Mobile;
                    MC_TB_Primary_Contact_LN.Text = customer.Primary_Contact_LN;
                    MC_TB_Primary_Contact_Email.Text = customer.Primary_Contact_Email;
                    MC_TB_Webstie.Text = customer.Customer_Website;
                    MC_TB_Accounts_Payable_FN.Text = customer.Accounts_Payable_FN;
                    MC_TB_Accounts_Payable_LN.Text = customer.Accounts_Payable_LN;
                    MC_TB_Accounts_Payable_Email.Text = customer.Accounts_Payable_EML;
                    TB_Customer_Notes.Text = customer.Customer_Notes;
                    TB_Customer_Name.Text = customer.Customer_Name;
                    TBRT_Customer_ID.Text = customer.Customer_ID.ToString();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void MC_TB_Customer_ABN_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.Customer customer = new BLL.Customer();
                if (!customer.Update(TB_Customer_Name.Text, MC_TB_Customer_Phone.Text, MC_TB_Customer_ABN.Text, MC_TB_Customer_Mob.Text, MC_TB_Primary_Contact_FN.Text,
                            MC_TB_Primary_Contact_LN.Text, MC_TB_Webstie.Text, MC_TB_Primary_Contact_Email.Text, MC_TB_Accounts_Payable_FN.Text, MC_TB_Accounts_Payable_LN.Text,
                            MC_TB_Accounts_Payable_Email.Text, TB_Customer_Notes.Text, TBRT_Customer_ID.Text))
                    MessageBox.Show("An error occurred during customer update");
                else
                    MessageBox.Show(TB_Customer_Name.Text + " has been updated");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TBRT_Customer_ID_TextChanged(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Do you really want to Delete the User " + this.TB_Customer_Name.Text + "?", "Delete Users", MessageBoxButtons.YesNo);
                if (dialog == DialogResult.Yes)
                {
                    BLL.Customer customer = new BLL.Customer();
                    if (!customer.Del(TBRT_Customer_ID.Text))
                        MessageBox.Show("An error occurred during customer delete");
                    else
                        MessageBox.Show(TB_Customer_Name.Text + " has been deleted");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void MC_TB_Customer_Phone_TextChanged(object sender, EventArgs e)
        {

        }

        private void MC_TB_Customer_Mob_TextChanged(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void MC_TB_Primary_Contact_FN_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void MC_TB_Primary_Contact_LN_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void MC_TB_Primary_Contact_Email_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void MC_TB_Webstie_TextChanged(object sender, EventArgs e)
        {

        }

        private void MC_TB_Accounts_Payable_FN_TextChanged(object sender, EventArgs e)
        {

        }

        private void MC_TB_Accounts_Payable_LN_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click_1(object sender, EventArgs e)
        {

        }

        private void MC_TB_Accounts_Payable_Email_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form_Modify_Customer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void BT_Sel_Click(object sender, EventArgs e)
        {
            using (var form = new Form_Sel_Customer())
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    Customer_Search(form.retCustomer_ID);
                }
            }
        }

        private void TB_Customer_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (TB_Customer_Name.Text.Length != 0)
                if (e.KeyCode == Keys.Enter)
                {
                    Customer_Search(null);
                }
        }
    }
}
    